
#include<iostream>
#include<map>
using namespace std;
int n,mod;
typedef long long LL;
map<string, int>mp;
LL ret = 0;
string s; int p;

bool cheak(char ch)
{
    if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
        return true;
    }
    else 
    {
        return false;
    }
}

int main()
{
    cin >> n >> mod;
    while (n--)
    {
        cin >> s >> p;
        mp[s] = p;
    }
    char ch;
    string t = " ";
    while (scanf("%c", &ch) != EOF)
    {
        if (cheak(ch))
        {
            t += ch;
        }
        else
        {
            ret = (ret + mp[t])% mod;
            t = " ";
        }
    }
    cout << ret<< endl;
    return 0;
}